package com.ruoyi.channel.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.channel.domain.bo.TChannelInfoBo;
import com.ruoyi.channel.domain.vo.TChannelInfoVo;
import com.ruoyi.channel.service.ITChannelInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 渠道管理
 *
 * @author zr
 * @date 2023-08-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/channel/channelInfo")
public class TChannelInfoController extends BaseController {

    private final ITChannelInfoService iTChannelInfoService;

    /**
     * 查询渠道管理列表
     */
    @SaCheckPermission("channel:channelInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TChannelInfoVo> list(TChannelInfoBo bo, PageQuery pageQuery) {
        return iTChannelInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出渠道管理列表
     */
    @SaCheckPermission("channel:channelInfo:export")
    @Log(title = "渠道管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TChannelInfoBo bo, HttpServletResponse response) {
        List<TChannelInfoVo> list = iTChannelInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "渠道管理", TChannelInfoVo.class, response);
    }


    @GetMapping("/channelList")
    public R<List<TChannelInfoVo>> channelList(TChannelInfoBo bo) {
        return R.ok(iTChannelInfoService.queryList(bo));
    }

    /**
     * 获取渠道管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("channel:channelInfo:query")
    @GetMapping("/{id}")
    public R<TChannelInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTChannelInfoService.queryById(id));
    }

    /**
     * 新增渠道管理
     */
    @SaCheckPermission("channel:channelInfo:add")
    @Log(title = "渠道管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TChannelInfoBo bo) {
        return toAjax(iTChannelInfoService.insertByBo(bo));
    }

    /**
     * 修改渠道管理
     */
    @SaCheckPermission("channel:channelInfo:edit")
    @Log(title = "渠道管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TChannelInfoBo bo) {
        return toAjax(iTChannelInfoService.updateByBo(bo));
    }

    /**
     * 删除渠道管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("channel:channelInfo:remove")
    @Log(title = "渠道管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTChannelInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
